package com.argrace.smart.activity;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.Nullable;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.util.Log;
import android.view.View;

import com.aliyun.alink.business.devicecenter.api.add.DeviceInfo;
import com.aliyun.alink.business.devicecenter.api.discovery.IDiscoveryListener;
import com.aliyun.alink.business.devicecenter.api.discovery.LocalDeviceMgr;
import com.argrace.smart.R;
import com.argrace.smart.adapter.DeviceAdapter;
import com.argrace.smart.base.BaseActivity;
import com.argrace.smart.ui.EmptyRecycleView;
import com.argrace.smart.util.AlinkHttpUtil;
import com.chad.library.adapter.base.BaseQuickAdapter;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by argrace_dev_1 on 2018/1/30.
 * 搜索网关界面
 */

public class SearchDeviceActivity extends BaseActivity implements SwipeRefreshLayout.OnRefreshListener, BaseQuickAdapter.RequestLoadMoreListener {
    EmptyRecycleView recyclerView;
    SwipeRefreshLayout refreshLayout;
    private DeviceAdapter adapter;
    private boolean isFoundDevice = false;
    private Handler handler = new Handler();
    private ArrayList<DeviceInfo> lists = new ArrayList<>();

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_searchdevice);
        init();
    }

    private void setView() {
        recyclerView = (EmptyRecycleView) findViewById(R.id.recyclerView);
        refreshLayout = (SwipeRefreshLayout) findViewById(R.id.refreshLayout);
        recyclerView.setItemAnimator(new DefaultItemAnimator());
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        adapter = new DeviceAdapter(lists, this);
        adapter.openLoadAnimation(BaseQuickAdapter.ALPHAIN);
        adapter.setListeners(new DeviceAdapter.OnAdapterItemClickListener() {
            @Override
            public void click(DeviceInfo bean, int position) {
                Intent intent = new Intent(SearchDeviceActivity.this, BindDeviceActivity.class);

                intent.putExtra("mac", bean.mac);
                intent.putExtra("sn", bean.sn);
                intent.putExtra("model", bean.model);
                Log.i("info", "SearchDeviceActivity中选中网关mac = " + bean.mac + "---sn = " + bean.sn + "---model = " + bean.model);
                startActivity(intent);


            }
        });
        recyclerView.setAdapter(adapter);
//        refreshLayout.setColorSchemeColors(getResources().getColor(R.color.colorTheme), getResources().getColor(R.color.colorTheme), getResources().getColor(R.color.colorTheme));//getResources().getColor(R.color.colorTheme), getResources().getColor(R.color.colorTheme), getResources().getColor(R.color.colorTheme)
        refreshLayout.setOnRefreshListener(this);
    }

    private void init() {
        setView();
        setTitle(true, false, "搜索wifi下网关");
        //进入搜索网关流程
        onRefresh();
    }

    //进行设备搜索
    private void searchDevice() {
        lists.clear();
        Log.i("info", "进入本地设备查询");
        LocalDeviceMgr.getInstance().startDiscovery(this, new IDiscoveryListener() {
            //发现本地局域网内设备
            @Override
            public void onLocalDeviceFound(DeviceInfo deviceInfo) {
                Log.i("info", "发现本地设备" + deviceInfo.toString());
                lists.add(deviceInfo);
                isFoundDevice = true;
                setRefreshing(false);
                adapter.loadMoreEnd();
                adapter.notifyDataSetChanged();
                //移除所有待执行任务
                handler.removeCallbacksAndMessages(null);
                LocalDeviceMgr.getInstance().stopDiscovery();
            }

            //零配待添加设备发现
            @Override
            public void onEnrolleeDeviceFound(List<DeviceInfo> list) {
                Log.i("info", "onEnrolleeDeviceFound");
                LocalDeviceMgr.getInstance().stopDiscovery();

            }

        });
        handler.postDelayed(new Runnable() {
            @Override
            public void run() {
                if (!isFoundDevice) {
                    isFoundDevice = false;
                    setRefreshing(false);
                    adapter.loadMoreEnd();
                    Log.i("info", "20s内没有搜索到设备");
                    toastActBtm("没有搜索到设备!");
                }

            }
        }, 20 * 1000);
    }

    @Override
    public void onRefresh() {
        searchDevice();
    }

    @Override
    public void onLoadMoreRequested() {
        searchDevice();
    }

    public void setRefreshing(final boolean refreshing) {
        refreshLayout.post(new Runnable() {
            @Override
            public void run() {
                refreshLayout.setRefreshing(refreshing);
            }
        });
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        handler.removeCallbacksAndMessages(null);
    }
}
